package gps;

import javax.microedition.location.Location;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;

import net.rim.device.api.gps.BlackBerryCriteria;
import net.rim.device.api.gps.BlackBerryLocationProvider;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
	private static double longitud;
	private static double latitud; 
	private static TextField tf1 = new TextField();	   
	private static TextField tf2 = new TextField();

	
    /**
     * Creates a new MyScreen object
     */
    public MyScreen()
    {        
       // Set the displayed title of the screen       
	   setTitle("MyTitle");
	   tf1.setText("Cargando...");	   
	   add(tf1);
	   add(tf2); 	 
	   
	   if(!MyScreen.currentLocation()){
		   tf1.setText("False");        	
   	   }
   	  
    }
    
    public void show(){
    	
       this.invalidate();
    }
    
    private static boolean currentLocation() {  
        boolean retval = true;  
       try {  
    	   BlackBerryCriteria myBlackBerryCriteria = new BlackBerryCriteria();
    	   
    	   /* BlackBerry extension to JSR 179 */
    	   BlackBerryLocationProvider myBlackBerryProvider =
    	       (BlackBerryLocationProvider) LocationProvider.getInstance(myBlackBerryCriteria); 
    	   
    	   Location local = myBlackBerryProvider.getLocation(3000);
    	   QualifiedCoordinates coord = local.getQualifiedCoordinates();
    	   longitud = coord.getLongitude();
    	   latitud = coord.getLatitude(); 
    	   tf1.setText("Latitud: "+latitud+" ("+coord.getVerticalAccuracy()+")");
    	   tf2.setText("Longitud: "+longitud+" ("+coord.getHorizontalAccuracy()+")");  	   
        } catch (Exception e) {   
            retval = false;
        }  
        return retval;  
    }  


}
